<!DOCTYPE html>
<html lang="cn">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./js/vue.js"></script>
</head>

<body>
  <!-- 父组件 -->
  <div id="app">
    <turnip> </turnip>
    <turnip ref="kang"> </turnip>
    <turnip> </turnip>
    <button @click='emit'>触发子组件</button>
  </div>
  <template id="com">
    <div>
      <p>我是子组件</p>
      <button @click='root'>获取父组件</button>
    </div>

  </template>
  <script>
    const app = new Vue({
      el: '#app',
      methods: {

        // 父访问子
        emit() {
          // 法一：不常用
          // 获取子组件组
          // let arrChildren = this.$children;
          // console.log(arrChildren);
          // // 调用子组件内的函数
          // this.$children[0].showChildren();
          // // for (let item of this.$children) console.log(item.msg);
          // this.$children.map(item => console.log(item.msg));

          // 法二：常用
          this.$refs.kang.showChildren();
          console.log(this.$refs.kang.msg)
        }
      },
      components: {
        turnip: {
          template: '#com',
          data() {
            return {
              msg: '子组件内的数据',
            };
          },
          methods: {
            showChildren() {
              console.log('子组件的方法')
            },
            root() {
              // this.$parent.emit();//不常用
              this.$root.emit();
            }
          },
        }
      }

    })
  </script>

</body>

</html>